#include <stdarg.h>
#include "Log.h"
#include "common.h"
#include "Timer.h"

namespace cy_univerX
{
namespace log
{

	CLogger& CLogger::Instance()
	{
		static CLogger g_logger;
		return g_logger;
	}

	void CLogger::Output(int level, const char* format, ...)
	{
		char str_buf[common::K_MaxBufferLen];
	
		va_list va;
		va_start(va, format);
		::_vsnprintf_s(str_buf, common::K_MaxBufferLen, format, va);
		va_end(va);
		str_buf[common::K_MaxBufferLen - 1] = '\0';
		std::string result(str_buf);

		m_OnOutput(level, result);
	}
//-----------------------------------------------------------------
	void CLogFile::OnLogOutput(int level, const std::string &str)
	{
		size_t n =  str.size();
		if (n)
		{
			std::string time;
			common::GetStringLocalTime(time);
			::fputs(time.c_str(), m_Stream);
			::fputs(" ", m_Stream);
			::fputs(str.c_str(), m_Stream);
			if (str[n - 1] != '\n')
			{
				::fputs("\n", m_Stream);
			}
		}
		::fflush(m_Stream);
	}

}
}